package qf.sail.service.impl;

import qf.sail.dao.GoodDao;
import qf.sail.dao.OrderItemDao;
import qf.sail.dao.impl.GoodDaoImpl;
import qf.sail.dao.impl.OrderItemDaoImpl;
import qf.sail.entity.Good;
import qf.sail.entity.OrderItem;
import qf.sail.service.ShopCarService;

import java.util.ArrayList;
import java.util.List;

public class ShopCarServiceImpl implements ShopCarService {

    private OrderItemDao orderItemDao = new OrderItemDaoImpl();

    private GoodDao goodDao= new GoodDaoImpl();

    /**
     * 插入购物车子项，返回id
     * @param
     * @return
     */
    @Override
    public Long addItemToShopCar(long goodId, long shopCarId, String size, String color, int count) {
        OrderItem orderItem = new OrderItem();

        orderItem.setGoodId(goodId);
        orderItem.setShopCarId(shopCarId);
        orderItem.setCount(count);
        orderItem.setSize(size);
        orderItem.setColor(color);

        return orderItemDao.addItemToShopCar(orderItem);
    }

    @Override
    public List<OrderItem> getItemsByShopCarId(long shopCarId) {

        List<OrderItem> items = new ArrayList<>();

        //获取商品信息
        for(OrderItem item:orderItemDao.getItemsByShopCarId(shopCarId)){
            Good good = goodDao.getGoodById(item.getGoodId());
            item.setGood(good);
            items.add(item);
        }

        return items;

    }
}
